package net.thxjj.adminServer.entity.aep.profilegc;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection="th_profile_gc_info_log")//集合名
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class ProfileGCInfoBean {
    private String deviceId;
    private long timestamp;
    private long TAC;
    private long SNR;
    private long RSRQ;
    private long RSRP;
    private long PCI;
    private long ECL;
    private long EARFCN;
    private long CSQ;
    private long Protocol_Number;
    private String Custom_Msg;
    private long Custom_Msg_Length;
    private UpdateBean1 updateBean1;
    private UpdateBean2 updateBean2;
    private UpdateBean3 updateBean3;
    private String alarmContent;


    public void transToBean(){
          switch ((int)getProtocol_Number()){
              case 1:
                  if(this.getCustom_Msg().length()>=12){
                      StringBuffer stringBuffer = new StringBuffer();
                      long battery_level = Long.parseLong(this.getCustom_Msg().substring(0,2),16);
                      long public_status = Long.parseLong(this.getCustom_Msg().substring(2,4),16);
                       boolean isDismantle = !((public_status & 0x80)==0);
                       if(isDismantle) stringBuffer.append("拆除报警、");
                      boolean isLifeStop = !((public_status & 0x20)==0);
                      if(isLifeStop) stringBuffer.append("寿命终止、");
                      boolean isLowBattery = !((public_status & 0x10)==0);
                      if(isLowBattery) stringBuffer.append("低电压报警、");
                      boolean isLowBNoDisturb = !((public_status & 0x08)==0);
                      if(isLowBNoDisturb) stringBuffer.append("低电压免打扰、");
                      boolean isSDComAbnormal= !((public_status & 0x04)==0);
                      if(isSDComAbnormal) stringBuffer.append("底板通信异常、");
                      boolean isLocalTestAlarm= !((public_status & 0x02)==0);
                      if(isLocalTestAlarm) stringBuffer.append("本地测试报警、");
                      boolean isHeartBeat= !((public_status & 0x01)==0);

                      long smoke_status = Long.parseLong(this.getCustom_Msg().substring(4,6),16);
                      boolean isSmokeAlarm = !((smoke_status & 0x40)==0);
                      if(isSmokeAlarm) stringBuffer.append("烟雾报警、");
                      boolean isSmokeSilence= !((smoke_status & 0x20)==0);
                      if(isSmokeSilence) stringBuffer.append("烟雾报警静音、");
                      boolean isSensorFault= !((smoke_status & 0x10)==0);
                      if(isSensorFault) stringBuffer.append("烟雾传感器故障、");
                      boolean isSensorFaultNoDisturb= !((smoke_status & 0x08)==0);
                      if(isSensorFaultNoDisturb) stringBuffer.append("烟雾传感器故障免打扰、");

                      long R_concentration = Long.parseLong(this.getCustom_Msg().substring(6,8),16);
                      long IR_concentration = Long.parseLong(this.getCustom_Msg().substring(8,10),16);
                      long temperature = Long.parseLong(this.getCustom_Msg().substring(10,12),16);
                      if(temperature>127) temperature = temperature-256;


                      this.updateBean1 = new UpdateBean1().setBattery_Level(battery_level).setDismantle(isDismantle).setLifeStop(isLifeStop)
                              .setLowBattery(isLowBattery)
                              .setLowBNoDisturb(isLowBNoDisturb)
                              .setSDComAbnormal(isSDComAbnormal)
                              .setLocalTestAlarm(isLocalTestAlarm)
                              .setHeartBeat(isHeartBeat)
                              .setSmokeAlarm(isSmokeAlarm)
                              .setSmokeSilence(isSmokeSilence)
                              .setSensorFault(isSensorFault)
                              .setSensorFaultNoDisturb(isSensorFaultNoDisturb)
                              .setR_concentration(R_concentration).setIR_concentration(IR_concentration).setTemperature(temperature);

                      this.alarmContent = stringBuffer.toString();
                  }
                  break;
              case 2:
                  if(this.getCustom_Msg().length()>=26){
                      StringBuffer stringBuffer = new StringBuffer();
                      long public_status1 = Long.parseLong(this.getCustom_Msg().substring(2,4),16);

                      boolean isHeartBeat = !((public_status1 & 0x01)==0);
                      boolean isLocalTestAlarm = !((public_status1 & 0x02)==0);
                      if(isLocalTestAlarm) stringBuffer.append("本地测试报警、");
                      boolean isRemoteTestAlarm = !((public_status1 & 0x04)==0);
                      if(isRemoteTestAlarm) stringBuffer.append("远程测试报警、");
                      boolean isLowBattery = !((public_status1 & 0x08)==0);
                      if(isLowBattery) stringBuffer.append("低电压报警、");
                      boolean isLowBNoDisturb = !((public_status1 & 0x10)==0);
                      if(isLowBNoDisturb) stringBuffer.append("低电压报警免打扰、");
                      boolean isLifeStop = !((public_status1 & 0x20)==0);
                      if(isLifeStop) stringBuffer.append("寿命终止、");
                      boolean isDC = !((public_status1 & 0x40)==0);

                      long public_status2 = Long.parseLong(this.getCustom_Msg().substring(0,2),16);
                      boolean isSDComAbnormal = !((public_status2 & 0x01)==0);
                      if(isSDComAbnormal) stringBuffer.append("底板通讯异常、");

                      long gas_status = Long.parseLong(this.getCustom_Msg().substring(4,6),16);
                      boolean isGasSensorFaultNoDisturb = !((gas_status & 0x01)==0);
                      if(isGasSensorFaultNoDisturb) stringBuffer.append("GAS传感器故障免打扰、");
                      boolean isGasSensorFault = !((gas_status & 0x02)==0);
                      if(isGasSensorFault) stringBuffer.append("GAS传感器故障、");
                      boolean isGASSilence = !((gas_status & 0x04)==0);
                      if(isGASSilence) stringBuffer.append("GAS报警静音、");
                      boolean isGASAlarm = !((gas_status & 0x08)==0);
                      if(isGASAlarm) stringBuffer.append("GAS报警、");

                      long co_status = Long.parseLong(this.getCustom_Msg().substring(6,8),16);
                      boolean isCOSensorFaultNoDisturb = !((co_status & 0x01)==0);
                      if(isCOSensorFaultNoDisturb) stringBuffer.append("CO传感器故障免打扰、");
                      boolean isCOSensorFault = !((co_status & 0x02)==0);
                      if(isCOSensorFault) stringBuffer.append("CO传感器故障、");
                      boolean isCOSilence = !((co_status & 0x04)==0);
                      if(isCOSilence) stringBuffer.append("CO报警静音、");
                      boolean isCOAlarm = !((co_status & 0x08)==0);
                      if(isCOAlarm) stringBuffer.append("CO报警、");

                      long gasLevel = Long.parseLong(this.getCustom_Msg().substring(8,12),16);
                      long coLevel = Long.parseLong(this.getCustom_Msg().substring(12,16),16);

                      long temperature = Long.parseLong(this.getCustom_Msg().substring(16,18),16);
                      if(temperature>127) temperature = temperature-256;
                      long gasAD = Long.parseLong(this.getCustom_Msg().substring(18,22),16);
                      long coAD = Long.parseLong(this.getCustom_Msg().substring(22,26),16);

                      this.updateBean2 = new UpdateBean2().setHeartBeat(isHeartBeat)
                              .setLocalTestAlarm(isLocalTestAlarm)
                              .setRemoteTestAlarm(isRemoteTestAlarm)
                              .setLowBattery(isLowBattery)
                              .setLowBNoDisturb(isLowBNoDisturb)
                              .setLifeStop(isLifeStop)
                              .setDC(isDC)
                              .setSDComAbnormal(isSDComAbnormal)
                              .setGasSensorFaultNoDisturb(isGasSensorFaultNoDisturb)
                              .setGasSensorFault(isGasSensorFault)
                              .setGasSilence(isGASSilence)
                              .setGasAlarm(isGASAlarm)
                              .setCoSensorFaultNoDisturb(isCOSensorFaultNoDisturb)
                              .setCoSensorFault(isCOSensorFault)
                              .setCoSilence(isCOSilence)
                              .setCoAlarm(isCOAlarm)
                              .setGasLevel(gasLevel)
                              .setCoLevel(coLevel)
                              .setTemperature(temperature)
                              .setGasAD(gasAD)
                              .setCoAD(coAD);
                      this.alarmContent = stringBuffer.toString();
                  }
                  break;
              case 3:
                  if(this.getCustom_Msg().length()>=12){
                      long battery_level = Long.parseLong(this.getCustom_Msg().substring(0,2),16);
                      long temperature = Long.parseLong(this.getCustom_Msg().substring(2,4),16);
                      if(temperature>127) temperature = temperature-256;
                      long device_status = Long.parseLong(this.getCustom_Msg().substring(4,6),16);
                      long gasLevel = Long.parseLong(this.getCustom_Msg().substring(6,8),16);
                      long coLevel = Long.parseLong(this.getCustom_Msg().substring(8,12),16);
                      this.updateBean3 = new UpdateBean3().setBattery_level(battery_level)
                              .setDevice_status(device_status)
                              .setGasLevel(gasLevel)
                              .setCoLevel(coLevel)
                              .setTemperature(temperature);
                      switch ((int)device_status){
                          case 1:
                              this.alarmContent = "报警";
                              break;
                          case 2:
                              this.alarmContent = "报警静音";
                              break;
                          case 3:
                              this.alarmContent = "传感器故障";
                              break;
                          case 6:
                              this.alarmContent = "寿命终止";
                              break;
                          case 8:
                              this.alarmContent = "预热";
                              break;
                          case 9:
                              this.alarmContent = "测试报警";
                              break;
                          case 10:
                              this.alarmContent = "通讯故障";
                              break;
                      }
                  }
                  break;
          }


    }

    @Override
    public String toString() {
        return "ProfileGCInfoBean{" +
                "deviceId='" + deviceId + '\'' +
                ", timestamp=" + timestamp +
                ", TAC=" + TAC +
                ", SNR=" + SNR +
                ", RSRQ=" + RSRQ +
                ", RSRP=" + RSRP +
                ", PCI=" + PCI +
                ", ECL=" + ECL +
                ", EARFCN=" + EARFCN +
                ", CSQ=" + CSQ +
                ", Protocol_Number=" + Protocol_Number +
                ", Custom_Msg='" + Custom_Msg + '\'' +
                ", Custom_Msg_Length=" + Custom_Msg_Length +
                ", updateBean1=" + updateBean1 +
                ", updateBean2=" + updateBean2 +
                ", updateBean3=" + updateBean3 +
                '}';
    }
}
